home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / developer-tools / aros / source / exec / library / src / makelibrary.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-16  |  3.4 KB  |  134 lines

  1. /*
  2.     (C) 1995, 96 AROS - The Amiga Replacement OS
  3.     $Id$
  4.     $Log$
  5.     Desc:
  6.     Lang: english
  7. */
  8. #include "exec_intern.h"
  9. #include "//memory/include/memory.h"
  10. #include <aros/abscall.h>
  11. #include <exec/memory.h>
  12. #include <dos/dos.h>
  13.  
  14. /*****************************************************************************
  15.  
  16.     NAME */
  17.     #include <clib/exec_protos.h>
  18.  
  19.     __AROS_LH5(struct Library *, MakeLibrary,
  20.  
  21. /*  SYNOPSIS */
  22.     __AROS_LA(APTR,       funcInit,   A0),
  23.     __AROS_LA(APTR,       structInit, A1),
  24.     __AROS_LA(ULONG_FUNC, libInit,    A2),
  25.     __AROS_LA(ULONG,      dataSize,   D0),
  26.     __AROS_LA(BPTR,       segList,    D1),
  27.  
  28. /*  LOCATION */
  29.     struct ExecBase *, SysBase, 14, Exec)
  30.  
  31. /*  FUNCTION
  32.     Allocates memory for the library, builds it and calls the library's
  33.     init vector. Generally this function is for internal use and for
  34.     use by library programmers that don't want to use the automatic
  35.     initialization procedure.
  36.  
  37.     INPUTS
  38.     funcInit   - Either a pointer to an array of function offsets
  39.              (starts with -1, relative to funcInit) or to an array
  40.              of absolute function pointers.
  41.     structInit - Pointer to a InitStruct() data region or NULL.
  42.     libInit    - The library's init vector or NULL.
  43.              The init vector is called with the library address (D0),
  44.              the segList (A0) and ExecBase (A6) as arguments.
  45.              If the init fails the init code has to free the base memory
  46.              and return NULL (the library address for success).
  47.     dataSize   - Size of the library structure including system structures.
  48.              Must be at least sizeof(struct Library).
  49.     segList    - BCPL pointer to the library segments. Used to free the
  50.              library later.
  51.  
  52.     RESULT
  53.     The library base address or NULL.
  54.  
  55.     NOTES
  56.     The library base is always aligned to the maximum of sizeof(LONG)
  57.     and double alignment restrictions.
  58.  
  59.     EXAMPLE
  60.  
  61.     BUGS
  62.  
  63.     SEE ALSO
  64.     AddLibrary(), RemLibrary(), MakeFunctions(), InitStruct(), SumLibrary().
  65.  
  66.     INTERNALS
  67.  
  68.     HISTORY
  69.  
  70. ******************************************************************************/
  71. {
  72.     __AROS_FUNC_INIT
  73.  
  74.     struct Library *library;
  75.     ULONG negsize=0;
  76.  
  77.     /* Calculate the jumptable's size */
  78.     if(*(WORD *)funcInit==-1)
  79.     {
  80.     /* Count offsets */
  81.     WORD *fp=(WORD *)funcInit+1;
  82.     while(*fp++!=-1)
  83.         negsize+=LIB_VECTSIZE;
  84.     }
  85.     else
  86.     {
  87.     /* Count function pointers */
  88.     void **fp=(void **)funcInit;
  89.     while(*fp++!=(void *)-1)
  90.         negsize+=LIB_VECTSIZE;
  91.     }
  92.  
  93.     /* Align library base */
  94.     negsize=(negsize+(MEMCHUNK_TOTAL-1))&~(MEMCHUNK_TOTAL-1);
  95.  
  96.     /* Allocate memory */
  97.     library=(struct Library *)AllocMem(dataSize+negsize,MEMF_PUBLIC|MEMF_CLEAR);
  98.  
  99.     /* And initilize the library */
  100.     if(library!=NULL)
  101.     {
  102.     /* Get real library base */
  103.     library=(struct Library *)((char *)library+negsize);
  104.  
  105.     /* Build jumptable */
  106.     if(*(WORD *)funcInit==-1)
  107.         /* offsets */
  108.         MakeFunctions(library,(WORD *)funcInit+1,(WORD *)funcInit+1);
  109.     else
  110.         /* function pointers */
  111.         MakeFunctions(library,funcInit,NULL);
  112.  
  113.     /* Write sizes */
  114.     library->lib_NegSize=negsize;
  115.     library->lib_PosSize=dataSize;
  116.  
  117.     /* Create structure */
  118.     if(structInit!=NULL)
  119.         InitStruct(structInit,library,0);
  120.  
  121.     /* Call init vector */
  122.     if(libInit!=NULL)
  123.         library=__AROS_AC3(struct Library *,libInit,
  124.         __AROS_ACA(struct Library *,  library, D0),
  125.         __AROS_ACA(BPTR,              segList, A0),
  126.         __AROS_ACA(struct ExecBase *, SysBase, A6));
  127.     }
  128.     /* All done */
  129.     return library;
  130.  
  131.     __AROS_FUNC_EXIT
  132. } /* MakeLibrary */
  133.  
  134.